package com.ruoyi.util;

import lombok.extern.slf4j.Slf4j;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

import java.io.File;

@Slf4j
public class Image1Utils {
    static {
        String dir = System.getProperty("user.dir").replace("/", "\\");
        dir = dir.replaceAll("\\\\ruoyi-admin", "");
        log.info("Image1Utils.dir:==",dir);

        String filePath = "\\dll\\" + (System.getProperty("java.vm.name").contains("64") ? "x64" : "x86") + "\\opencv_java490.dll";
        String path = dir.concat(filePath);
        File file = new File(path);
        if (!file.exists()) {
            throw new RuntimeException("找不到动态库".concat(path));
        }
        System.load(path);
        System.out.println(path.concat("  load success"));
    }

    public static String copyImage() {
        String inputFilePath = "D:/opencv/1.png";

        String outputFilePath = "D:/opencv/2.jpg";
        File file = new File(inputFilePath);
        if (!file.exists()) {
            System.out.println("文件不存在: " + file.getAbsolutePath());
            return null;
        }

        System.out.println("1123213");
//        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        Mat srcImage = Imgcodecs.imread(inputFilePath);
        Mat dstImage = new Mat();
        Imgproc.cvtColor(srcImage, dstImage, Imgproc.COLOR_BGR2GRAY, 0);
        Imgcodecs.imwrite(outputFilePath, dstImage);
        return null;
    }
}
